package com.jbp.job;

import com.jbp.redis.RedisObjectSerializer;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;

import java.io.Serializable;
import java.util.Set;

public class LocalTask {
    public static void main(String[] args) {

    }

    //修改补池
    public static void changePatch(){
        String key = "haoya:luck:patchpool";
        ValueOperations<Serializable, Object> op = getTemplate().opsForValue();
        Integer patch = (Integer) op.get(key);
        patch = patch == null ? 0 : patch;
        patch = 3000000;
        op.set(key, patch);

        System.out.println("补池:"+op.get(key));
    }

    //初始化奖池放水
    public static void initPool(){
//        String[] pools={"D","A","B","C"};
        String[] pools={"A"};
        int[] prices = {20};
        ValueOperations<Serializable, Object> op = getTemplate().opsForValue();
        for (String poolName : pools) {
            for (int price : prices) {
                String keyCount = "haoya:luck:" + price + ":giftcount:" + poolName;
                String keyPool = "haoya:luck:" + price + ":pool:" + poolName;

                int giftCount = 20000;
                int pool = (int) (giftCount * price * 1.17 );
                pool += 20000;
                op.set(keyPool, pool);
                op.set(keyCount, giftCount);
                System.out.println("生成奖池：" + pool);
            }
        }
    }

    //设置奖池
    public static void setPool(){
        String poolName = "haoya:luck:" + 20 + ":pool:A";
        ValueOperations<Serializable, Object> op = getTemplate().opsForValue();

        Integer r_pool = (Integer) op.get(poolName);
        int pool = r_pool != null ? r_pool : 0;
        pool += 40000;
        op.set(poolName, pool);
        System.out.println("生成奖池：" + pool);
    }

    //清数据
    public static void clear(){
        //清所有数据
        String pattern = "*";
        //清新用户
//        redisConfigService.removePattern("user:new:*");
//        System.out.println("清奖池");
//        redisConfigService.removePattern("haoya:luck*");

        RedisTemplate<Serializable, Object> template = getTemplate();
        Set<Serializable> keys = template.keys(pattern);
        if (keys.size() > 0) {
            template.delete(keys);
        }
    }

    private static RedisTemplate<Serializable, Object> getTemplate() {
        String host = "r-8vbe21thnw4hfqx80ypd.redis.zhangbei.rds.aliyuncs.com";
        RedisTemplate<Serializable, Object> template = new RedisTemplate<>();
        JedisConnectionFactory fac = new JedisConnectionFactory(new JedisPoolConfig());
        JedisShardInfo shardInfo = new JedisShardInfo(host, 6379);
        shardInfo.setPassword("n0Bwszka");
        fac.setShardInfo(shardInfo);
        fac.setDatabase(3);
        template.setConnectionFactory(fac);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new RedisObjectSerializer());
        template.afterPropertiesSet();
        return template;
    }
}
